package com.moon.admin.flowable.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moon.admin.flowable.domain.FlowFormModelQuery;
import com.moon.admin.flowable.entity.FlowFormModelEntity;
import com.moon.admin.flowable.service.FlowFormModelService;
import com.moon.admin.framework.mybatisplus.query.Condition;
import com.moon.admin.framework.secure.annotation.FormRepeat;
import com.moon.admin.framework.secure.annotation.PreAuth;
import com.moon.admin.framework.springmvc.domain.R;
import com.moon.admin.framework.syslog.annotation.SysLog;
import com.moon.admin.framework.util.ToolUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 自定义表单
 * @author ninglong
 * @create 2020-09-27 14:15
 */
@RestController
@RequestMapping("/flowable/form")
@Api(value = "自定义表单接口", tags = "自定义表单接口")
public class FlowableFormModelController {

	@Autowired
    private FlowFormModelService formModelService;

    /**
     * 分页 自定义表单
     */
    @PostMapping("list")
    @PreAuth("flowable_form_view")
    @ApiOperation(value = "分页显示 自定义表单", notes = "传入FormModel")
    public R<IPage<FlowFormModelEntity>> list(@RequestBody FlowFormModelQuery formModel) {
    	LambdaQueryWrapper<FlowFormModelEntity> wrappers = Condition.getQueryWrapper(new FlowFormModelEntity(), formModel);
        IPage<FlowFormModelEntity> pages = formModelService.page(Condition.getPage(formModel),wrappers);
        return R.success(pages);
    }

    /**
     * 保存表单
     */
    @RequestMapping(value="save",method={RequestMethod.POST})
    @PreAuth({"flowable_form_add","flowable_form_edit","flowable_form_desgin"})
	@SysLog(mod = "自定义表单管理", fun = "保存表单")
	@FormRepeat
    @ApiOperation("保存表单")
    public R<Boolean> save(@RequestBody FlowFormModelEntity formModel){
        List<FlowFormModelEntity> formModels = formModelService.list(Wrappers.<FlowFormModelEntity>lambdaQuery().select(FlowFormModelEntity::getId).eq(FlowFormModelEntity::getFormKey,formModel.getFormKey()));
        if(formModels.size()>1){
            return R.fail("表单key不能重复");
        }
        if(formModel.getId()==null && formModels.size()>0){
        	return R.fail("表单key不能重复");
        }
        if(formModel.getId()!=null && formModels.size()>0 && formModels.get(0).getId().longValue()!=formModel.getId().longValue()){//编辑的时候也不能为空
        	return R.fail("表单key不能重复");
        }
        //TODO 未考虑对已被引用的表单进行修改操作
        return R.status(formModelService.saveOrUpdate(formModel));
    }

	@GetMapping(value = "removeByIds")
	@PreAuth("flowable_form_remove")
	@SysLog(fun="自定义表单管理",mod="删除流程表单")
	@FormRepeat
	public R<Void> removeByIds(String ids) {
		if(ToolUtils.isBlank(ids)) {
			return R.fail("请选择要删除的流程类型");
		}
		//TODO 未考虑对已被引用的表单进行修改操作
		return R.status(formModelService.removeByIds(ToolUtils.toIntList(ids)));
	}

    /**
     * 获取任务节点的自定义表单信息
     */
    @RequestMapping(value = "getFormInfo",method = {RequestMethod.GET})
    public R<Map<String, Object> > getFormInfoByTaskIdAndProcessInstanceId(String taskId, String processInstanceId){
        return R.success(formModelService.getFormInfoByTaskIdAndProcessInstanceId(taskId,processInstanceId));
    }

    /**
     * 获取开始节点的自定义表单信息
     */
    @RequestMapping(value = "getStartFormInfo",method = {RequestMethod.GET})
    public R<Map<String, Object> > getStartFormInfoByDeploymentId(String deploymentId){
        return R.success(formModelService.getStartFormInfoByDeploymentId(deploymentId));
    }
}